<!-- 修改样品/添加样品 -->
<template>
	<view class="page" style="margin-top:30px;margin: 30upx;">
		<view class="uni-list">
			<view class="uni-list-cell-divider" style="align-items: center;" v-if="type == 1" @click="selectType">
				<text class="width-a100">类型：</text>
				<view class="arrow-box width-auto">
					<view class="bottom">
						<text class="bottom-arrow1"></text>
						<text class="bottom-arrow2"></text>
					</view>
				</view>
			</view>
			<view class="uni-list-cell" v-if="type == 1"><input type="text" v-model="sampleType.name" readonly /></view>

			<view class="uni-list-cell-divider" style="align-items: center;" @click="selectLocation">
				<text class="width-a100">采样点：</text>
				<view class="arrow-box width-auto">
					<view class="bottom">
						<text class="bottom-arrow1"></text>
						<text class="bottom-arrow2"></text>
					</view>
				</view>
			</view>
			<view class="uni-list-cell"><input type="text" v-model="location" /></view>

			<view class="uni-list-cell-divider" @click="goSampleName">
				样品名称：
				<view class="uni-list-cell-navigate uni-navigate-right"></view>
			</view>
			<view class="uni-list-cell">
				<!-- {{sampleName.selectItem.checkVal && sampleName.selectItem.checkVal+'--'}}{{sampleName.name || ''}} -->
				<input type="text"  v-model="sampleName.name" />
			</view>

			<view class="uni-list-cell-divider">份数：</view>
			<view class="uni-list-cell"><input type="text" v-model="number"/></view>

			<view v-if="typeB == '1'||type == '1'">
				<view class="uni-list-cell-divider">采样天数：</view>
				<view class="uni-list-cell"><input type="text" v-model="date_num" /></view>

				<view class="uni-list-cell-divider">
					<text class="width-auto">频次：</text>
					<view class="check-box">
						<checkbox-group @change="checkboxChange">
							<label>
								<checkbox :value="'1'" :checked="twoCode.length == 1" />
								二级编码
							</label>
						</checkbox-group>
					</view>
				</view>
				<view class="uni-list-cell"><input type="text" v-model="rate_num" /></view>
			</view>

			<view class="uni-list-cell-divider" @click="goSamplingStandard">
				采样标准：
				<view class="uni-list-cell-navigate uni-navigate-right"></view>
			</view>
			<view class="uni-list-cell">{{ samplingStandard.selectItem.checkVal || '' }}</view>

			<view class="uni-list-cell-divider" @click="goCheckItem">
				检测项目：
				<view class="uni-list-cell-navigate uni-navigate-right"></view>
			</view>
			<!--  #ifdef MP-WEIXIN -->
			<view class="uni-list-cell">{{ checkItem.selectItem.checkVal + '--' + checkItem.selectItemsStr }}</view>
			<!--  #endif -->
			<!--  #ifdef APP-PLUS -->
			<view class="uni-list-cell"><text v-html="checkItem.selectItem.checkVal + '--' + checkItem.selectItemsStr"></text></view>
			<!--  #endif -->	
		</view>

		<view class="btn-row uni-flex"><button type="primary" class="primary uni-flex-item" @click="addSample()">提交</button></view>

		<wzp-picker :mode="'one'" ref="wzpPicker" :pickerList="pickerValueArray" @onConfirm="onConfirm"></wzp-picker>
		<!-- @onCancle="onCancle" -->
	</view>
</template>

<script>
import wzpPicker from '../../components/wzp-picker/wzpPicker.vue';
import utils from '../../common/utils.js';
import store from '../../store/index.js';

import net from '../../net.js';
	import uniIcons from "@/components/uni-icons/uni-icons.vue"
const ADDSAMPLE = 'ADDSAMPLE';

export default {
	components: {
		wzpPicker,
		uniIcons
	},
	data() {
		return {
			typeB:'',
			sampleId: '',
			codeId: '',
			type: null,
			pickerValueArray: [],
			locationArray: [],
			sampleTypeArray: [{ name: '新样', id: '1' }, { name: '平行样', id: '3' }, { name: '实验室比对', id: '6' }, { name: '空白样', id: '5' }],
			sampleType: { name: '平行样', id: '3' },
			wzpType: null,
			checked: false,
			location: '', // 采样点
			date_num: '1', // 天数
			number: '1', // 份数
			rate_num: '1', // 频次
			twoCode: [], // 二级编号
			enumData: {}, // 枚举数据
			samplingStandard: {
				// 检测标准
				checkList: [],
				selectItem: {
					checkVal: '',
					checkId: '',
					index: []
				}
			},
			checkItem: {
				// 检测项目
				checkList: [],
				selectItems: [],
				classifyId: [],
				selectItem: {
					checkVal: '',
					checkId: '',
					index: []
				},
				selectItemsStr: ''
			},
			sampleName: {
				// 采样名称
				checkList: [],
				selectItem: {
					checkVal: '',
					checkId: '',
					index: []
				},
				name: ''
			},
			resData: null
		};
	},
	onLoad(res) {
		console.log(res);
		if (res.data) {
			this.resData = JSON.parse(res.data);
			let title = '修改样品';
			if (res.type == '1') title = '添加样品';
			uni.setNavigationBarTitle({ title: title });
		} else {
			this.sampleType = this.sampleTypeArray[0];
		}
		this.type = res.type;
			
		if(res.typeB){
			this.typeB = res.typeB;
		}
		
		console.log(this.type);
		console.log(this.typeB);
		this.codeId = res.codeId;
		this.getInfo();
	},
	onBackPress(event){
		console.log(event,"2");
	},
	methods: {
		initData(data) {
			data = data || {};

			this.sampleId = data.id;

			if (data.item_id && Array.isArray(data.item_id)) {
				let selectItemsArr = [];
				data.item_id.forEach(item => {
					this.checkItem.selectItems.push(item.id);
					this.checkItem.classifyId.push(item.classify_id);
					selectItemsArr.push(item.name);
				});
				this.checkItem.selectItemsStr = selectItemsArr.join(',');
			}

			if (data.item_type_id && data.item_type_id.id) {
				let itemCheckObj = {
					checkList: [],
					check: null
				};
				utils.getCheckList(this.enumData['item'], data.item_type_id.id, itemCheckObj);
				if (itemCheckObj.check) this.checkItem.selectItem = itemCheckObj.check;
				if (itemCheckObj.checkList.length != 0) this.checkItem.checkList = itemCheckObj.checkList;
			}

			if (data.sampling_standard && data.sampling_standard.id) {
				let ssCheckObj = {
					checkList: [],
					check: null
				};
				utils.getCheckList(this.enumData['sample_standard_type'], data.sampling_standard.id, ssCheckObj);
				if (ssCheckObj.check) this.samplingStandard.selectItem = ssCheckObj.check;
				if (ssCheckObj.checkList.length != 0) this.samplingStandard.checkList = ssCheckObj.checkList;
			}

			if (data.breed_type_id && data.breed_type_id.id) {
				let breedCheckObj = {
					checkList: [],
					check: null
				};
				utils.getCheckList(this.enumData['breed'], data.breed_type_id.id, breedCheckObj);
				if (breedCheckObj.check) this.sampleName.selectItem = breedCheckObj.check;
				if (breedCheckObj.checkList.length != 0) this.sampleName.checkList = breedCheckObj.checkList;
			}
			this.date_num = data.date_num || '1';
			this.location = data.location_id || '';
			if (data.two_code != 0 && data.two_code != null) {
				this.twoCode = data.two_code;
			}
			this.number = data.number || '1';
			this.rate_num = data.rate_num || '1';
			this.sampleName.name = data.sample_name ||'';
			console.log(this.sampleName);
		},
		getInfo() {
			net.netGet('/module/api/main/getSampleRelateInfo', {}, res => {
				this.enumData = res;
				this.enumData.location &&
					this.enumData.location.forEach((item, index) => {
						this.locationArray.push({ name: item.location });
					});
				if (this.resData) {
					this.initData(this.resData);
				}
			});
		},
		addSample() {
			let location = this.location;
			let sampleName = this.sampleName.name;
			let date_num = this.date_num;
			let number = this.number;
			let rate_num = this.rate_num;
			let selectItem = this.samplingStandard.selectItem;
			let twoCode = 0;

			if (this.type == 1 && !this.sampleType.id) {
				uni.showToast({ icon: 'none', title: '类型不能为空' });
				return;
			}

			if (!date_num.length) {
				uni.showToast({ icon: 'none', title: '天数不能为空' });
				return;
			}

			if (!number.length) {
				uni.showToast({ icon: 'none', title: '份数不能为空' });
				return;
			}

			if (!rate_num.length) {
				uni.showToast({ icon: 'none', title: '频次不能为空' });
				return;
			}

			if (!this.checkItem.selectItem.checkVal.length) {
				uni.showToast({ icon: 'none', title: '检测项目不能为空' });
				return;
			}

			if (!this.checkItem.selectItemsStr.length) {
				uni.showToast({ icon: 'none', title: '项目不能为空' });
				return;
			}

			if (!this.codeId) {
				uni.showToast({ icon: 'none', title: '数据错误，无法提交' });
				return;
			};

			let data = {
				cyd: [location],
				sample_name: { 1: [sampleName] },
				sample_type: this.sampleType.id,
				date_num: { 1: [date_num] },
				fs: { 1: [number] },
				rate_num: { 1: [rate_num] },
				pid: { 1: [''] },
				type: [],
				two_code: { 1: [this.twoCode.length] },
				sampling_standard: { 1: [selectItem.checkId] },
				item_type_id: { 1: { 1: [this.checkItem.selectItem.checkId] } },
				item: { 1: { 1: { 1: this.checkItem.selectItems } } },
				classify_id: { 1: { 1: { 1: this.checkItem.classifyId } } },
			};

			if (this.sampleName.selectItem.checkId) {
				data.pid['1'] = [this.sampleName.selectItem.checkId];
			}

			if (this.type == 1 && this.sampleType.id != 1) {
				data['relate_sample_id'] = [this.sampleId];
			}

			let url = '/module/api/main/addSample/';
			let modalContent = '添加';

			if (this.sampleId && this.type != 1) {
				data['ids'] = { 1: [this.sampleId] };
				url = '/module/api/main/editSample/';
				modalContent = '修改';
			}

			console.log(JSON.stringify(data));

			for (let o in data) {
				data[o] = JSON.stringify(data[o]);
			}

			net.netPost(url + encodeURIComponent(this.codeId), data, res => {
				let msg = modalContent + (res == 'success' ? '成功！' : '失败！');
				console.log(res,"我是res");
				uni.showModal({
					title: '提示',
					content: msg,
					showCancel: false,
					success: modalRes => {
						if (res == 'success') uni.navigateBack({ delta: 1 });
					}
				});
			});
		},
		goSampleName() {
			let param = utils.toParam({
				data: this.enumData['breed'],
				showName: 'NAME',
				checkList: this.sampleName.checkList,
				name: (this.sampleName.name || '')
			});
			console.log(this.sampleName.name);
			uni.navigateTo({
				url: './check_item' + param
			});
			utils.setCallBack((checkList, selectItem, value) => {
				if (checkList[0] && checkList[0].checkVal) {
					this.sampleName.checkList = checkList;
				}
				this.sampleName.name = value;
				if (selectItem != null && Object.keys(selectItem).length) {
					this.sampleName.selectItem = selectItem;
				}
			});
		},
		goSamplingStandard() {
			let param = utils.toParam({
				data: this.enumData['sample_standard_type'],
				showName: 'STANDARD',
				checkList: this.samplingStandard.checkList,
				selectItem: this.samplingStandard.selectItem
			});
			
			uni.navigateTo({
				url: './check_item' + param,
			});
			utils.setCallBack((checkList, selectItem) => {
				console.log(checkList, selectItem);
				if (checkList[0] && checkList[0].checkVal) {
					this.samplingStandard.checkList = checkList;
				}
				if (selectItem != null && Object.keys(selectItem).length) {
					this.samplingStandard.selectItem = selectItem;
				}
			});
		},
		goCheckItem() {
			let param = utils.toParam({
				data: this.enumData['item'],
				showName: 'ITEM', // ITEM
				checkList: this.checkItem.checkList,
				selectItems: this.checkItem.selectItems,
				selectItem: this.checkItem.selectItem
			});

			uni.navigateTo({
				url: './check_item' + param,
			});

			utils.setCallBack((checkList, selectItems, selectItem, selectItemsStr, classifyId) => {
				this.checkItem.checkList = checkList;
				this.checkItem.selectItems = selectItems;
				this.checkItem.selectItem = selectItem;
				this.checkItem.selectItemsStr = selectItemsStr;
				this.checkItem.classifyId = classifyId;
				//console.log(this.checkItem);
			});
		},
		checkboxChange(e) {
			this.twoCode = e.detail.value;
		},
		//切换类型
		selectType() {
			this.wzpType = 1;
			this.pickerValueArray = this.sampleTypeArray;
			this.$refs.wzpPicker.showPicker();
		},
		selectLocation() {
			this.wzpType = 2;
			this.pickerValueArray = this.locationArray;
			this.$refs.wzpPicker.showPicker();
		},
		onCancel(value) {
			console.log(value);
		},
		onConfirm(data) {
			if (this.wzpType == 2) {
				this.location = data.value[0].name;
			} else {
				this.sampleType = data.value[0] || {};
			}
		},
		countItemTypeId() {}
	}
}
</script>

<style>
	.page{
		width: 100%;
	}
.uni-card {
	width: calc(100% - 40rpx);
}

.uni-list-cell {
	min-height: 80upx;
	padding: 15upx;
	font-size: 32upx;
}

.uni-list-cell:after {
	left: 0;
	height: 0;
}

.uni-list-cell input {
	width: 100%;
	font-size: 32upx;
	border-bottom: 1px solid #999;
}
</style>
